/*
 * The MIT License 
 *
 * Copyright (c) 2011 pf-miles
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package action.expr;

/**
 * some random methods for test
 * 
 * @author pf-miles 2011-6-22 上午10:30:11
 * 
 */
public class RandUtil {
    // generate 1..i random numbers
    public static int rand(int i) {
        int j = (int) (Math.random() * (i + 1));
        if (j == 0)
            j = 1;
        return j;
    }

    public static String randKey() {
        StringBuilder sb = new StringBuilder();
        // first: a-zA-Z$_
        int r1 = rand(4);
        switch (r1) {
        case 1:
            sb.append((char) (97 + rand(26) - 1));
            break;
        case 2:
            sb.append((char) (65 + rand(26) - 1));
            break;
        case 3:
            sb.append("$");
            break;
        case 4:
            sb.append("_");
            break;
        }
        // other: a-zA-Z0-9$_\.:
        int r2 = rand(50);
        for (int i = 0; i < r2; i++) {
            r1 = rand(7);
            switch (r1) {
            case 1:
                sb.append((char) (97 + rand(26) - 1));
                break;
            case 2:
                sb.append((char) (65 + rand(26) - 1));
                break;
            case 3:
                sb.append((char) (48 + rand(10) - 1));
                break;
            case 4:
                sb.append("$");
                break;
            case 5:
                sb.append("_");
                break;
            case 6:
                sb.append(".");
                break;
            case 7:
                sb.append(":");
                break;
            }
        }
        return sb.toString();
    }

    public static String randNum() {
        StringBuilder sb = new StringBuilder();
        int r1 = rand(2);
        switch (r1) {
        case 1:
            // positive number
            break;
        case 2:
            // negative number
            sb.append("-");
            break;
        }
        int r2 = rand(50);
        for (int i = 0; i < r2; i++) {
            sb.append((char) (48 + rand(10) - 1));
        }
        r1 = rand(2);
        switch (r1) {
        case 1:
            // no dot
            break;
        case 2:
            // dot
            sb.append(".");
            r2 = rand(50);
            for (int i = 0; i < r2; i++) {
                sb.append((char) (48 + rand(10) - 1));
            }
            break;
        }
        return sb.toString();
    }

    public static String randStr() {
        StringBuilder sb = new StringBuilder();
        sb.append("'");
        int r2 = rand(2);
        switch (r2) {
        case 1:
            // normal str
            char c = (char) (rand(65536) - 1);
            while (isCharShouldEscaped(c))
                c = (char) (rand(65536) - 1);
            sb.append(c);
            break;
        case 2:
            // escaped char
            int r1 = rand(7);
            switch (r1) {
            case 1:
                // \b
                sb.append("\\b");
                break;
            case 2:
                // \t
                sb.append("\\t");
                break;
            case 3:
                // \n
                sb.append("\\n");
                break;
            case 4:
                // \f
                sb.append("\\f");
                break;
            case 5:
                // \r
                sb.append("\\r");
                break;
            case 6:
                // \'
                sb.append("\\'");
                break;
            case 7:
                // \\
                sb.append("\\\\");
                break;
            }
            break;
        }
        sb.append("'");
        return sb.toString();
    }

    private static boolean isCharShouldEscaped(char c) {
        return '\b' == c || '\t' == c || '\n' == c || '\f' == c || '\r' == c || '\\' == c || '\'' == c;
    }
}
